package com.hopes.bizz.position.position.support.impl;

import cn.hutool.core.util.StrUtil;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.bizz.position.position.bo.RtPositionBO;
import com.hopes.bizz.position.position.dto.ReceiveRtPositionDTO;
import com.hopes.bizz.position.position.support.RedisRtPositionHandle;
import com.hopes.boss.common.core.constant.CommonConstants;
import com.hopes.boss.common.data.tenant.TenantBroker;
import com.hopes.boss.common.entity.ResolverBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * @author yumr
 * @ProjectName bizz
 * @Description 实时人员定位数据处理
 * @time 2024/2/21 17:58
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RedisPersonRtPositionHandle implements RedisRtPositionHandle, InitializingBean, DisposableBean {

    public static final String CHANNEL = "person-rt-position-channel";


    private ScheduledThreadPoolExecutor threadPoolExecutor;

    @Override
    public void destroy() throws Exception {
        threadPoolExecutor.shutdown();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("person-rt-position-").build();
        threadPoolExecutor = new ScheduledThreadPoolExecutor(1, threadFactory);
        threadPoolExecutor.scheduleWithFixedDelay(new TimerTask() {
            @Override
            public void run() {
                TenantBroker.runAs(CommonConstants.TENANT_ID_1, (tenantId) -> {

                    //log.debug("定时任务处理实时定位数据结果：{}， 处理历史定位数据：{}", handleCacheRes, handleQueueRes);
                });
            }
        }, 5, 5, TimeUnit.SECONDS);
    }


    /**
     * 处理
     * @param receiveRtPosition 对象
     */
    @Override
    public void handleResolver(ReceiveRtPositionDTO receiveRtPosition) {
        final boolean res = this.beforeHandle(receiveRtPosition);
        if (!res) {
            return;
        }
        TenantBroker.runAs(CommonConstants.TENANT_ID_1, (tenantId) -> {
            final ResolverBO<RtPositionBO> handleRes = this.handlePersonRtPerson(receiveRtPosition.getData());

        });
    }

    private boolean beforeHandle(ReceiveRtPositionDTO receiveRtPosition) {
        return false;
    }

    private ResolverBO<RtPositionBO> handlePersonRtPerson(List<RtPositionBO> rtPositionList) {
        final ResolverBO<RtPositionBO> bo = new ResolverBO<>();
        final List<RtPositionBO> saveList = new ArrayList<>();
        final List<ErrorMessage> errorMessageList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;
        for (RtPositionBO rtPosition : rtPositionList) {

            String validateRes = ValidatorUtils.validateEntity(rtPosition);

            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(rtPosition);
            } else {
                failedNum++;
//                errorMessageList.add(new ErrorMessage(rtPosition.getLineNum(), new HashSet<String>() {{
//                    add(validateRes);
//                }}));
            }
        }
        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }


    @Override
    public String channel() {
        return RedisPersonRtPositionHandle.CHANNEL;
    }

    @Override
    public Class<ReceiveRtPositionDTO> messageClass() {
        return ReceiveRtPositionDTO.class;
    }



}
